<template>

  <el-container>
    <el-aside width="245px">
      <div class="home-aside">
        <img src="../assets/img/logo.png" alt="">
        <span>值班后台</span>
      </div>
      <el-row class="home-menu">
        <el-col :span="12">
          <el-menu router :default-active="activePath" class="el-menu-vertical-demo" width='245px;'>
            <el-menu-item :index="item.path+''" v-for="item in menulist" :key="item.id" @click="saveNavState(item.path)"
              :class="item.path === activePath ? 'iscolor' : 'nocolor'">
              <i :class="item.icon"></i>
              <span>{{item.name}}</span>
            </el-menu-item>
          </el-menu>
        </el-col>
      </el-row>
    </el-aside>
    <el-container>
      <el-header height="90px">
        <div class="img-user">
          <el-dropdown @command="gowhere">
            <span class="el-dropdown-link">
              <img src="../assets/img/小黄鸡.png" alt style="cursor:pointer" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="b">个人中心</el-dropdown-item>
              <el-dropdown-item command="a">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="title">
          <div>当前在线人数{{num}}</div>
          <div>{{currentTime | formatDate}}</div>
        </div>

      </el-header>
      <el-main>
        <router-view @peoples="setpeople"></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>
<script>
  export default {
    // 组件私有数据
    data() {
      return {
        // 当前时间
        currentTime: Date.parse(new Date()),
        // 当前总人数
        num:0,
        // 自懂选中当前的页面
        activePath: '/sign',
        // 存放侧边栏数据
        menulist: [{
            path: '/sign',
            name: '是否在线',
            icon: 'el-icon-s-management'
          },
          {
            path: '/msg',
            name: '消息通知',
            icon: 'el-icon-chat-line-square'
          },
          {
            path: '/company',
            name: '单位管理',
            icon: 'el-icon-user'
          },
          {
            path: '/message',
            name: '通知公告',
            icon: 'el-icon-bell'
          },
          {
            path: '/dataTotal',
            name: '数据统计',
            icon: 'el-icon-pie-chart'
          },
        ],
      }
    },
    filters: {
      formatDate: function (value) {
        let date = new Date(value);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m;
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
      }
    },
    // 生命周期函数
    created() {
      this.getPermission()
      var _this = this; //声明一个变量指向Vue实例this，保证作用域一致
      this.timer = setInterval(function () {
        _this.currentTime = //修改数据date
          new Date().getFullYear() +
          "-" +
          (new Date().getMonth() + 1) +
          "-" +
          new Date().getDate() +
          " " +
          new Date().getHours() +
          ":" +
          new Date().getMinutes() +
          ": " +
          new Date().getSeconds();
      }, 1000);
    },

    methods: {
      gowhere(com) {
        if (com == "a") {
          this.$router.push('/person');
        } else {
          window.sessionStorage.clear()
          this.$router.push('/login');
        }
      },
      getPermission() {
        this.activePath = window.sessionStorage.getItem('activePath')
      },
      // 保存路由
      //点击一级标题，高亮
      saveNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath);
        this.activePath = activePath
      },
      // 设置当前人数
      setpeople(val){
        this.num = val;
      }
    },
    // 监听路由改变
    watch: {
      '$route.path': function () {
        this.activePath = this.$route.path
        window.sessionStorage.setItem('activePath', this.$route.path)
      }
    },
    beforeDestroy() {
      if (this.timer) {
        clearInterval(this.timer); // 在Vue实例销毁前，清除我们的定时器
      }
    }
  }
</script>
<style lang="less" scoped>
  .el-main {
    background: rgba(238, 238, 238, 1);
  }

  .el-header,
  .el-aside {
    background: #fff;
  }

  .el-header {
    z-index: 100;
    box-shadow: 0 5px 7px 0px rgba(0, 0, 0, 0.1);
  }

  .el-aside {
    box-shadow: 5px 0 7px 0px rgba(0, 0, 0, 0.1);
    z-index: 100;
    height: 937px;
  }

  .home-aside {
    border-bottom: 3px solid rgba(35, 131, 240, .66);
    margin: 20px 20px 0 20px;
    padding: 0 0 10px 0;

    span {
      font-size: 24px;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.9);
      line-height: 60px;
      vertical-align: top;
      margin-left: 20px;
    }
  }

  .title {
    width: 200px;
    font-size: 14px;
    float: right;
    margin-top: 20px;
    text-align: center;
    line-height: 26px;
  }

  .img-user {
    float: right;
    margin-top: 20px;
    width: 62px;
    height: 62px;
    padding: 8px 13px;
    box-sizing: border-box;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 0px 7px 0px rgba(0, 0, 0, 0.1);
    border-radius: 50%;

  }

  .el-col {
    width: 100%;
    text-align: center;
    margin-top: 40px;
  }

  .el-menu {

    font-size: 18px !important;
    font-weight: 400;
    line-height: 64px;
    border: 0;

    .el-menu-item {
      font-size: 18px !important;
      font-weight: 400;
    }
  }

  .iscolor {
    color: rgba(35, 131, 240, 0.9);
  }

  .nocolor {
    color: rgba(0, 0, 0, 0.9);
  }
</style>